package com.lotus.crm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lotus.crm.dao.CustomerRepository;
import com.lotus.crm.domain.Customer;
import com.lotus.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	@Resource
	private CustomerRepository customerRepository;

	@Override
	public List<Customer> findNoAssociationCustomers() {
		return customerRepository.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaId) {
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public void associateCustomersToFixedArea(String customerIdStr, String fixedAreaId) {
		String[] ids = StringUtils.split(customerIdStr, ",");
		if("null".equals(fixedAreaId)){
			fixedAreaId = null;
		}
		for (String id : ids) {
			customerRepository.updateFixedAreaIdById(Integer.valueOf(id),fixedAreaId);
		}
	}

	@Override
	public void save(Customer customer) {
		customerRepository.save(customer);
	}

	@Override
	public Customer findByTelephone(String telephone) {
		return customerRepository.findByTelephone(telephone);
	}

	@Override
	public void updateTypeByTelephone(String telephone) {
		customerRepository.updateTypeByTelephone(telephone);
	}

}
